﻿/************************************************************************************
* Copyright (c) 2019 All Rights Reserved.
*命名空间：Separate.Model
*文件名： Plc
*创建人： Liebeskind
*创建时间：2019/6/11 17:38:37
*描述
*=====================================================================
*修改标记
*修改时间：2019/6/11 17:38:37
*修改人：Liebeskind
*描述：
************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Commonality.Model;
using Commonality.Model.PLCControl;

namespace Separate.Model
{
    public static class PLC
    {
        private static ImplementLADEx PlcLoop;
        private static ImplementLADEx AutoLoop;
        private static event ImplementLADEx AutoLoopEvent;
        #region 标量
        /// <summary>
        /// 已经捕获状态
        /// </summary>
        public static short ScalarCapture = 100;

        /// <summary>
        /// 进入异常
        /// </summary>
        public static short ScalarAbnormal = 200;
        #endregion

        //站点1-主线区
        /// <summary>
        /// 入料口Bufff处扫码触发：1:触发扫码；2:流向分盘；3:作为异常流出分盘
        /// 1：剔除错误入料；
        /// 2：获取本摞Model以分类封盖
        /// </summary>
        public static TPLCCell dEntrance_SQRC_A;
        /// <summary>
        /// 入料口Bufff处扫码触发：1:触发扫码；2:流向分盘；3:作为异常流出分盘
        /// 1：剔除错误入料；
        /// 2：获取本摞Model以分类封盖
        /// </summary>
        public static TPLCCell dEntrance_SQRC_B;

        /// <summary>
        /// Buff转移分盘信号
        /// </summary>
        public static TPLCCell mBuff2Sepa_A;
        /// <summary>
        /// Buff转移分盘信号
        /// </summary>
        public static TPLCCell mBuff2Sepa_B;

        /// <summary>
        /// 分盘视觉处理触发：1：识别是否是封盖；2：识别tray和产品异常
        /// </summary>
        public static TPLCCell mTakeImage_A;
        /// <summary>
        /// 分盘视觉处理触发：1：识别是否是封盖；2：识别tray和产品异常
        /// </summary>
        public static TPLCCell mTakeImage_B;

        /// <summary>
        /// 分盘扫码：
        /// 1：将产品DataList存入数据库；
        /// 2：对比本盘中两块产品DataList是否一致；
        /// 3：将DataList对比结果和视觉处理结果并存到产品信息中以剔除异常；
        /// 4：识别是否是封盖，两个扫码枪都扫码超时
        /// </summary>
        public static TPLCCell dSeparate_SQRC_A;
        /// <summary>
        /// 分盘扫码：
        /// 1：将产品DataList存入数据库；
        /// 2：对比本盘中两块产品DataList是否一致；
        /// 3：将DataList对比结果和视觉处理结果并存到产品信息中以剔除异常；
        /// 4：识别是否是封盖，两个扫码枪都扫码超时
        /// </summary>
        public static TPLCCell dSeparate_SQRC_B;

        /// <summary>
        /// 视觉结果变量结果立即提交PLC帮助缓存
        /// </summary>
        public static TPLCCell dVisionResult2PLC_A;         //D410
        /// <summary>
        /// 视觉结果变量结果立即提交PLC帮助缓存
        /// </summary>
        public static TPLCCell dVisionResult2PLC_B;         //D420

        /// <summary>
        /// 分盘扫码时从PLC读取视觉结果提交给Mysql
        /// </summary>
        public static TPLCCell dVisionResult2Mysql_A;         //D411
        /// <summary>
        /// 分盘扫码时从PLC读取视觉结果提交给Mysql
        /// </summary>
        public static TPLCCell dVisionResult2Mysql_B;         //D421

        /// <summary>
        /// 测高IO_A1
        /// </summary>
        public static TPLCCell dHeight_A1;
        /// <summary>
        /// 测高IO_A2
        /// </summary>
        public static TPLCCell dHeight_A2;
        /// <summary>
        /// 测高IO_B1
        /// </summary>
        public static TPLCCell dHeight_B1;
        /// <summary>
        /// 测高IO_B2
        /// </summary>
        public static TPLCCell dHeight_B2;


        /// <summary>
        /// 上料机蜂鸣器
        /// </summary>
        public static TPLCCell yBuzzer_P1;
        /// <summary>
        /// 空Tray收集机蜂鸣器
        /// </summary>
        public static TPLCCell yBuzzer_P2;
        private static void InitPLCCell()
        {
            CPLCControl.InitPlcCell(out dEntrance_SQRC_A, ESignalType.SIG_D, 1, 0, 500);
            CPLCControl.InitPlcCell(out dEntrance_SQRC_B, ESignalType.SIG_D, 1, 0, 510);

            CPLCControl.InitPlcCell(out mBuff2Sepa_A, ESignalType.SIG_M, 1, 0, 160);
            CPLCControl.InitPlcCell(out mBuff2Sepa_B, ESignalType.SIG_M, 1, 0, 180);

            CPLCControl.InitPlcCell(out mTakeImage_A, ESignalType.SIG_M, 1, 0, 501);
            CPLCControl.InitPlcCell(out mTakeImage_B, ESignalType.SIG_M, 1, 0, 511);

            CPLCControl.InitPlcCell(out dVisionResult2PLC_A, ESignalType.SIG_D, 1, 0, 410);
            CPLCControl.InitPlcCell(out dVisionResult2PLC_B, ESignalType.SIG_D, 1, 0, 420);

            CPLCControl.InitPlcCell(out dVisionResult2Mysql_A, ESignalType.SIG_D, 1, 0, 411);
            CPLCControl.InitPlcCell(out dVisionResult2Mysql_B, ESignalType.SIG_D, 1, 0, 421);


            CPLCControl.InitPlcCell(out dSeparate_SQRC_A, ESignalType.SIG_D, 1, 0, 502);
            CPLCControl.InitPlcCell(out dSeparate_SQRC_B, ESignalType.SIG_D, 1, 0, 512);


            CPLCControl.InitPlcCell(out yBuzzer_P1, ESignalType.SIG_Y, 1, 7, 10);
            CPLCControl.InitPlcCell(out yBuzzer_P2, ESignalType.SIG_Y, 2, 7, 10);

            CPLCControl.InitPlcCell(out dHeight_A1, ESignalType.SIG_D, 1, 0, 505);
            CPLCControl.InitPlcCell(out dHeight_A2, ESignalType.SIG_D, 1, 0, 506);

            CPLCControl.InitPlcCell(out dHeight_B1, ESignalType.SIG_D, 1, 0, 515);
            CPLCControl.InitPlcCell(out dHeight_B2, ESignalType.SIG_D, 1, 0, 516);

        }
        public static void PLCInit(ImplementLADEx al)
        {
            InitPLCCell();

            CPLCControl.workstationFirstAddress = 1;
            CPLCControl.workstationNumOffset = 2;
            PlcLoop = new ImplementLADEx(PlcImplement);
            CPLCControl.SetLADProcessEx(PlcLoop);

            AutoLoop = new ImplementLADEx(al);
            AutoLoopEvent += AutoLoop;

            PLCOpen();
        }
        private static void PLCOpen()
        {
            Log4Net.MsgInfo("站点1-上料机:" + CPLCControl.OpenPLC(1));
            //Log4Net.MsgInfo("站点2-空Tray收集机:" + CPLCControl.OpenPLC(2));            
        }
        public static void PLCClose()
        {
            CPLCControl.ClosePLC(1);
            //CPLCControl.ClosePLC(2);
        }
        private static void PlcImplement()
        {


            //
            AutoLoopEventFunction();
        }
        private static void AutoLoopEventFunction()
        {
            if (AutoLoopEvent != null)
            {
                AutoLoopEvent();
            }
        }
    }
}
